home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / lha / appinfo next >
Text File  |  1995-03-09  |  11KB  |  364 lines

  1.  
  2.                         LhA V1.32 Application Info
  3.  
  4.                             File structure and
  5.                                 algorithms.
  6.  
  7.                          By Stefan Boberg 1991,92
  8.  
  9.     NB: This is an early version of the document, so it is not complete.
  10.  
  11.  
  12. Format of a LZH / LHA file
  13. --------------------------
  14.  
  15.   LHA files have exactly the same file format and structure as LZH files,
  16. but LHA files generally are compressed with -lh5- compression, while LZH
  17. files generally are -lh1- compressed (see section about compression algo-
  18. rithms).
  19.  
  20.   Files can be stored in arbitrary order in the archive file.
  21.  
  22.   The overall file format is as follows:
  23.  
  24.    [file header]
  25.    [file data]
  26.    [file header]
  27.    [file data]
  28.    .
  29.    .
  30.    .
  31.    [archive terminator]
  32.  
  33.  
  34.  The file header is layout as follows:
  35.  
  36.    Case 1:  (header level 0)
  37.  
  38.       Header size        (in bytes)        1 byte
  39.       Header checksum                      1 byte
  40.       Storage method                       5 bytes
  41.       Compressed size                      4 bytes
  42.       Original size                        4 bytes
  43.       Last mod file date & time            4 bytes
  44.       File attributes                      1 byte
  45.       Header level                    [0]  1 byte
  46.       Filename length                      1 byte
  47.       Filename & filenote                  variable size
  48.       File CRC-16                          2 bytes
  49.  
  50.    Case 2:  (header level 1)
  51.  
  52.       Header size        (in bytes)        1 byte
  53.       Header checksum                      1 byte
  54.       Storage method                       5 bytes
  55.       Compressed size                      4 bytes
  56.       Original size                        4 bytes
  57.       Last mod file date & time            4 bytes
  58.       File attributes                      1 byte
  59.       Header level                    [1]  1 byte
  60.       Filename length                      1 byte
  61.       Filename & filenote                  variable size
  62.       File CRC-16                          2 bytes
  63.       Host Operating System                1 byte
  64.  
  65.         Extension size                     2 bytes
  66.         Extension data                     variable size
  67.         ...
  68.         Extension terminator          [0]  2 bytes
  69.  
  70.    Case 3:  (header level 2)
  71.  
  72.       Header size        (in bytes)        2 bytes
  73.       Storage method                       5 bytes
  74.       Compressed size                      4 bytes
  75.       Original size                        4 bytes
  76.       Last mod file date & time (UNIX-Fmt) 4 bytes
  77.       File attributes                      1 byte
  78.       Header level                    [1]  1 byte
  79.       Filename length                      1 byte
  80.       Filename & filenote                  variable size
  81.       File CRC-16                          2 bytes
  82.       Host Operating System                1 byte
  83.  
  84.         Extension size                     2 bytes
  85.         Extension data                     variable size
  86.         ...
  87.         Extension terminator          [0]  2 bytes
  88.  
  89.  
  90.  The compressed file data follows immediately after the last header byte.
  91.  
  92.  The archive terminator is a single 0 byte after the last data byte of the
  93.  last file in the archive.
  94.  
  95.  
  96.   Explanation of fields
  97.   ---------------------
  98.  
  99.     All fields are encoded in Intel-format, i.e. 16-bit quantities are stored
  100.   with the least significant byte first. 32-bit quantities are stored as two
  101.   16-bit Intel words with the least significant word first.
  102.  
  103.  
  104.   Header size
  105.  
  106.      This unsigned byte contains the length of the header excluding the
  107.    header checksum byte and the header size byte itself.
  108.  
  109.      With level-1 headers, the extended headers are NOT included in the
  110.    header size count. (except for the first two-byte length word).
  111.  
  112.      With level-2 headers, this is a two-byte word field containing the
  113.    length of the entire header including all extended headers.
  114.  
  115.   Header checksum
  116.  
  117.      This byte contains the modulo-256 checksum of the header, which is
  118.    calculated as follows (pseudo-C):
  119.  
  120.      {
  121.        unsigned byte header[];
  122.        unsigned byte length;
  123.        unsigned byte checksum;
  124.  
  125.        checksum = 0;
  126.        length   = header[0];        /* Header size field */
  127.  
  128.        while (length) {
  129.          checksum += header[length + 2];
  130.          length--;
  131.        }
  132.  
  133.        /* checksum now contains the checksum */
  134.      }
  135.  
  136.  
  137.   Storage method
  138.  
  139.      This is a 5-byte ASCII char array containing the storage method ID.
  140.    See the section about compression methods for a list of IDs.
  141.  
  142.  
  143.   Compressed size
  144.   Original size
  145.  
  146.      These 4-byte fields contains the size of the file in it's compressed
  147.    and original state, respectively.
  148.  
  149.  
  150.   Last file modification date & time
  151.  
  152.      The date and time is encoded in standard MS-DOS format. The 32-bit word
  153.    is divided into bit fields like this:
  154.  
  155.       Bit 31 - 25  (Year - 1980)
  156.           21 - 24  Month         [1..12]
  157.           16 - 20  Day           [1..31]
  158.           11 - 15  Hour          [0..23]
  159.            5 - 10  Minute        [0..59]
  160.            0 -  4  Seconds/2     [0..29]
  161.  
  162.  
  163.       With level-2 headers, things are a bit different. In this case the date
  164.     is stored in UNIX-format. A UNIX timestamp is a 32-bit integer containing
  165.     the number of seconds since January 1, 1970.
  166.  
  167.  
  168.   File attributes
  169.  
  170.      This byte field contains the file attribute bits, the format depends
  171.    on the host operating system.
  172.  
  173.  
  174.   Header level
  175.  
  176.      This byte field is used to indicate what kind of header this is, it
  177.    can currently be 0 (original LhArc format), 1 or 2 (Unix LHarc/LHA
  178.    format).
  179.  
  180.  
  181.   Filename length
  182.  
  183.      This field contains the length (in bytes) of the filename.
  184.  
  185.      Amiga LhArc/LhA stores filenotes in level-0 headers in the filename
  186.    field. The filenote follows the null-terminated filename (the filename
  187.    is not normally null-terminated). The length of the filenote and the
  188.    null byte should be included in the filename length count. This way
  189.    of storing the filenotes is compatible with all versions of LhArc, so
  190.    Amiga LZH archives with filenotes can be processed on other platforms
  191.    without problems.
  192.  
  193.  
  194.   Filename & filenote
  195.  
  196.      This field contains the filename and (optional) filenote.
  197.  
  198.  
  199.   File CRC-16
  200.  
  201.      This field contains the CRC-16 of the source (uncompressed) file.
  202.    It is used to check the integrity of the archive during extract and
  203.    test operations.
  204.  
  205. CRC
  206. ---
  207.  
  208.   The CRC is a standard ANSI 16-bit CRC. It is calculated as follows:
  209.  
  210.   (Pseudo-C)
  211.  
  212.   unsigned short calcCRC(unsigned char *buffer, unsigned int length)
  213.   {
  214.     unsigned short crc;
  215.     unsigned int i;
  216.     unsigned char c;
  217.  
  218.     crc = 0;
  219.     i = 0;
  220.  
  221.     while(i < length;) {
  222.         c = buffer[i++];
  223.         crc = crctable[(crc ^ (c)) & 0xFF] ^ (crc >> 8);
  224.     }
  225.     return(crc);
  226.   }
  227.  
  228.   The CRC-table is built as follows:
  229.  
  230.   unsigned short crctable[256];
  231.  
  232.   void make_crctable(void)
  233.   {
  234.     unsigned int i, j, r;
  235.  
  236.     for (i = 0; i < 256; i++) {
  237.       r = i;
  238.       for (j = 0; j < 8; j++)
  239.         if (r & 1) r = (r >> 1) ^ 0xA001;
  240.         else       r >>= 1;
  241.         crctable[i] = r;
  242.     }
  243.   }
  244.  
  245. Extended headers
  246. -----------------
  247.  
  248.   The `extended headers' are used in level-1 and level-2 headers to store
  249. optional or variably-sized information such as filenotes, operating-system
  250. specific attributes etc. The general structure of an extended header is:
  251.  
  252.      Length      [2 bytes]            (The length count includes the type,
  253.      Type        [1 byte]              length and data fields, i.e. data
  254.      Data        [Length - 3 bytes]    field length + 3 = Length)
  255.  
  256.   The extended-headers block is terminated by 2 zero bytes (zero length).
  257.  
  258.   The currently implemented headers are:
  259.  
  260.   Type
  261.   ----
  262.  
  263.      0         Common header   (Data = Header CRC16)
  264.  
  265.      1         Filename header (Data = ASCII string of Filename, excluding
  266.                                 directory names)
  267.  
  268.      2         Dirname header  (Data = ASCII string of Directory name,
  269.                                 excluding trailing slash). Node delimiter
  270.                                 is 0xFF (octal 0377, decimal 255)
  271.  
  272.   0x40         Attribute header (Data = Two-byte word containing file
  273.                                  attributes). This overrides the attribute
  274.                                  field in the main header.
  275.  
  276.   0x71         Filenote header (Data = ASCII string of filenote)
  277.  
  278.  
  279. Compression modes
  280. -----------------
  281.  
  282.   Currently, a file can be stored in the archive in one of four ways; it
  283. can be STORED (not compressed) or FROZEN (compressed) in three different
  284. ways. The method ID's are listed in the table below:
  285.  
  286.                             Method       ID
  287.                            ------------------
  288.                             Stored     -lh0-
  289.                             Frozen     -lh1-
  290.                             Frozen     -lh4-
  291.                             Frozen     -lh5-
  292.                             Directory  -lhd-
  293.                            ------------------
  294.  
  295.  I. STORED
  296.  
  297.   Compression:
  298.  
  299.     A stored file is not compressed. The file data should be copied
  300.   directly from the source file to the archive, the CRC16 for the file
  301.   must be calculated and stored in the header for data integrity check.
  302.  
  303.   Decompression:
  304.  
  305.     A stored file is not compressed. The file data can be copied
  306.   directly from the archive to the destination, while calculating the
  307.   CRC16 for the file.
  308.  
  309.  II. FROZEN (-lh1-)
  310.  
  311.    Compression:
  312.  
  313.      LZ77 with 4096 bytes window. Literals and copies encoded with
  314.    dynamic order-0 Huffman codes. Distance codes encoded with fixed
  315.    order-0 Huffman codes.
  316.  
  317.      [ No algorithm description in this early document version ]
  318.  
  319.    Decompression:
  320.  
  321.      [ No algorithm description in this early document version ]
  322.  
  323.  III. FROZEN (-lh4-)
  324.  
  325.    Compression:
  326.  
  327.      This method is exactly the same as -lh5-, but with a window size
  328.    of 4096 characters. See the description of -lh5- for more info.
  329.  
  330.    Decompression:
  331.  
  332.      This method is exactly the same as -lh5- and can be decompressed
  333.    with the same decompression routine, there is no difference between
  334.    -lh5- and -lh4- from the decompressor's point of view.
  335.  
  336.  IV. FROZEN (-lh5-)
  337.  
  338.    Compression:
  339.  
  340.      LZ77 with 8192 bytes window. Literals and copies encoded with
  341.    block-adaptive order-0 Huffman codes. Number of distance bits encoded
  342.    with another set of block-adaptive Huffman codes.
  343.  
  344.      [ No algorithm description in this early document version ]
  345.  
  346.    Decompression:
  347.  
  348.      No buffer initialization required.
  349.  
  350.      [ No algorithm description in this early document version ]
  351.  
  352.  V. Directory (-lhd-)
  353.  
  354.    Compression:
  355.  
  356.      No Compression. Set the CRC-16 field to 0000. The directory name
  357.    should include a trailing slash. (like in `dir1/dir2/', and not
  358.    `dir1/dir2')
  359.  
  360.    Decompression:
  361.  
  362.      No Compression. Just create the directory whose name is in the
  363.    filename field.
  364.